package com.ay.selfservicemachine.http.download;

import androidx.annotation.NonNull;

import com.ay.selfservicemachine.utils.HQWFileUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class DownloadExecute {



    Timer timer;
    File mFile;
    long mContentLength;
    DownloadListener downloadListener;
    public void startTimer(){
        if (timer!=null){
            timer.cancel();
        }
        timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (downloadListener!=null&&mFile!=null){
                    downloadListener.onDownloading(mFile, mFile.length(), mContentLength, HQWFileUtil.getPrintSize(mFile.length()), HQWFileUtil.getPrintSize(mContentLength));
                }

            }
        },0,1000);
    }
    public void clearTimer(){
        if (timer!=null){
            timer.cancel();
        }
    }


    public void startDownloadFile(String url, File file, DownloadListener downloadListener) {
        Call call = DownloadConfig.getInstance().getCall(url);
        this.downloadListener=downloadListener;
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                clearTimer();
                if (downloadListener!=null){
                    downloadListener.onFailed(file, e);
                }
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                InputStream is = null;
                FileOutputStream out = null;
                try {
                    ResponseBody responseBody = response.body();
                    long contentLength = responseBody.contentLength();
                    is = responseBody.byteStream();
                    out = new FileOutputStream(file);
                    byte[] buffer = new byte[1024];
                    int len = -1;
                    startTimer();
                    while ((len = is.read(buffer)) != -1) {
                        out.write(buffer, 0, len);
                        mFile=file;
                        mContentLength=contentLength;
//                        callBack.onDownloading(file, file.length(), contentLength, HQWFileUtil.getPrintSize(file.length()), HQWFileUtil.getPrintSize(contentLength));
                    }
                    call.cancel();
                    clearTimer();
                    if (downloadListener!=null){
                        downloadListener.onSucceed(file);
                    }

                } catch (Exception v) {
                    call.cancel();
                    is.close();
                    if (out!=null){
                        out.flush();
                        out.close();
                    }
                    clearTimer();
                    if (downloadListener!=null){
                        downloadListener.onFailed(file, v);
                    }

                }
            }
        });

    }

}
